package com.example.ch05memoryuserdetails.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @description:
 * @author:ScorpioWang
 * @date:2021-11-02-09-09
 */
@Configuration
public class ApplicationConfig {
    //这里必须重写PasswordEncoder方法
    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }

    @Bean
    public UserDetailsService userDetailsService() {
        //向内存中存入数据
        PasswordEncoder encoder = this.passwordEncoder();
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("admin").password(encoder.encode("admin")).roles(new String[]{"ADMIN", "USER"}).build());
        manager.createUser(User.withUsername("wang").password(encoder.encode("123")).roles(new String[]{"USER"}).build());
        return manager;
    }
}
